<?php


namespace app\index\controller;
use think\Controller;
use think\Db;

class Bukf extends Controller
{

    public function index()
    {
        $oldTime='2020-08-20';  //参考的那天
        $newTime='2020-08-23'; //需要补扣费那天


        $rows=Db::table('tb_money')
            ->where(['keyid'=>['>',0]])
//            ->where(['keyid'=>18058])
            ->whereTime('intime', 'between', [$oldTime.' 00:00:00', $oldTime.' 23:59:59'])
//            ->limit(1)
            ->select();


        $total_money=0;
        foreach ($rows as $k => $v) {
            print_r($v['Name'].$v['intime'].'<br>');
//            print_r($v['Name'].str_replace($oldTime,$newTime,$v['intime']));


            $money=$v['money'];

            $total_money += $money;

//////      更新该词 消费金额，扣费状态更改
            Db::table('tb_webkey')->where(['id' => $v['keyid']])->update(['moneypay'=>Db::raw('moneypay+'.$money)]);

//          插入tb_money 表
            Db::table('tb_money')->insert(['userID' => $v['userID'], 'webid' => $v['webid'], 'keyid' => $v['keyid'], 'seoid' => $v['seoid'], 'keypm' => $v['keypm'], 'typeid' => $v['typeid'], 'Name' => $v['Name'], 'money' => $money, 'qfstate' => $v['qfstate'], 'intime' => str_replace($oldTime,$newTime,$v['intime'])]);

//                    更新用户余额，消费金额
            Db::table('tb_user')->where('id', $v['userID'])->update(['moneypay'=>Db::raw('moneypay+'.$money), 'money'=>Db::raw('money-'.$money)]);
            Db::table('tb_admin')->where(['id' => $v['seoid']])->update(['M_money'=>Db::raw('M_money+'.$money)]);
            print($v['id'] . ':扣费'.$money.'----<br>');
        }

        print($total_money.'<br>');
        print('完成扣费');
    }



}